import { client } from "@/config/oss.js";
import { nanoid } from "nanoid";

const tool = {
  oss: {
    async upload(file) {
      const uuid = nanoid();
      const index = file.name.lastIndexOf(".");
      //由扩展名判断类型
      const extension = file.name.substring(index + 1).toLowerCase();
      console.log("inex" + index);
      let filename;
      if (extension == "png" || extension == "jpg") {
        const suffix = file.name.substring(index + 1);
        filename = `news_pic/${uuid}.${suffix}`;
      } else {
        const suffix = file.name.substring(index + 1);
        filename = `news_file/${uuid}.${suffix}`;
      }
      // 此处添加返回值
      const uploadResult = await client.multipartUpload(filename, file, {
        progress: function (p) {
          console.log("进度", p);
        }
      });
      // 假设 uploadResult 包含上传结果，可以返回它
      return { filename, uploadResult };
    }
  }
};
export default tool;
